<!-- 质检溯源的详情组件 -->
<template>
  <div class="app-container">
    <el-page-header @back="goBack" content="质检溯源详情"></el-page-header>质检溯源

    

    <!-- OQC信息 -->
    <div class="info-section">
      <div class="section-title">检测信息</div>
      <div v-loading="loading.oqc">
        <el-empty v-if="!oqcInfo || oqcInfo.length === 0" description="暂无OQC信息"></el-empty>
        <el-table v-else :data="oqcInfo" border>
          <el-table-column prop="oqc_code" label="OQC编码"></el-table-column>
          <el-table-column prop="oqc_name" label="OQC名称"></el-table-column>
          <el-table-column prop="quantity_out_num" label="出货数量"></el-table-column>
          <el-table-column prop="quantity_check_num" label="检验数量"></el-table-column>
          <el-table-column prop="quantity_quanlified_num" label="合格数量"></el-table-column>
          <el-table-column prop="quantity_unqualified_num" label="不合格数量"></el-table-column>
          <el-table-column prop="check_result" label="检验结果"></el-table-column>
        </el-table>
      </div>
    </div>

    <!-- 产品销售信息 -->
    <div class="info-section">
      <div class="section-title">产品制造加工信息</div>
      <div v-loading="loading.sales">
        <el-empty v-if="!salesInfo || salesInfo.length === 0" description="暂无产品销售信息"></el-empty>
        <el-table v-else :data="salesInfo" border>
          <el-table-column prop="sales_code" label="出货单号"></el-table-column>
          <el-table-column prop="order_code" label="销售订单号"></el-table-column>
          <el-table-column prop="product_name" label="产品名称"></el-table-column>
          <el-table-column prop="work_order_code" label="工单号"></el-table-column>
          <el-table-column prop="work_order_name" label="工单名称"></el-table-column>
          <el-table-column prop="qty_num" label="工单数量"></el-table-column>
          <el-table-column prop="plan_start_time" label="计划开始时间"></el-table-column>
          <el-table-column prop="plan_end_time" label="计划结束时间"></el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getLotTraceabilityOqc,
  getLotTraceabilitySales
} from "@/api/qc/PendingTasks";

export default {
  name: "LotTraceabilityDetail",
  data() {
    return {
      salesId: null,
      loading: {
        oqc: false,
        sales: false,
      },
      oqcInfo: [],
      salesInfo: [],
    };
  },
  created() {
    // The immediate watcher on `$route` will handle the initial data fetch.
  },
  watch: {
    '$route': {
      handler(newRoute) {
        const salesId = newRoute.params.salesId || newRoute.query.salesId;
        
        if (salesId) {
          // Fetch data only if salesId is present and has changed
          if (salesId !== this.salesId) {
            this.salesId = salesId;
            this.fetchData();
          }
        } else {
          console.warn('未找到出货单ID参数');
        }
      },
      immediate: true
    }
  },
  methods: {
    fetchData() {
      this.getOqcInfo();
      this.getSalesInfo();
    },
    // 获取OQC信息
    getOqcInfo() {
      if (!this.salesId) return;
      this.loading.oqc = true;
      getLotTraceabilityOqc(this.salesId)
        .then(res => {
          this.oqcInfo = Array.isArray(res) ? res : [];
        })
        .catch(err => {
          console.error('获取OQC信息失败:', err);
          this.$message.error('获取OQC信息失败');
          this.oqcInfo = [];
        })
        .finally(() => {
          this.loading.oqc = false;
        });
    },
    // 获取产品销售信息
    getSalesInfo() {
      if (!this.salesId) return;
      this.loading.sales = true;
      getLotTraceabilitySales(this.salesId)
        .then(res => {
          this.salesInfo = Array.isArray(res) ? res : [];
        })
        .catch(err => {
          console.error('获取产品销售信息失败:', err);
          this.$message.error('获取产品销售信息失败');
          this.salesInfo = [];
        })
        .finally(() => {
          this.loading.sales = false;
        });
    },
    // 返回列表页
    goBack() {
      this.$router.push({ path: "/qc/LotTraceability" });
    }
  }
};
</script>

<style scoped>
.batch-info-header {
  margin: 20px 0;
  padding: 10px;
  background-color: #f8f9fa;
  border-left: 4px solid #409EFF;
  font-size: 16px;
  font-weight: bold;
}

.info-section {
  margin-bottom: 20px;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  padding: 10px 0;
  margin-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
  color: #303133;
}

.info-content {
  display: flex;
  flex-wrap: wrap;
  padding: 10px;
  background-color: #fcfcfc;
}

.info-item {
  width: 33.33%;
  padding: 8px 0;
  display: flex;
}

.info-label {
  color: #606266;
  font-weight: bold;
  margin-right: 10px;
  min-width: 100px;
}

.info-value {
  color: #303133;
}

@media screen and (max-width: 1200px) {
  .info-item {
    width: 50%;
  }
}

@media screen and (max-width: 768px) {
  .info-item {
    width: 100%;
  }
}
</style>
